package com.itheima.security;

import cn.hutool.crypto.digest.MD5;
import com.itheima.pojo.User;
import com.qiniu.util.Md5;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyUserService implements UserDetailsService {
    //模拟数据库中的用户数据
    public Map<String, User> map = new HashMap<>();

    public void initData(){
        com.itheima.pojo.User user1 = new com.itheima.pojo.User();
        user1.setUsername("admin");
        user1.setPassword("$2a$10$BTcW94KOLQ5LhaT1OcduaunjELMiN7OoOZ1fD.U05Vd.Am21/jeUu");

        com.itheima.pojo.User user2 = new com.itheima.pojo.User();
        user2.setUsername("xiaoming");
        user2.setPassword("$2a$10$BTcW94KOLQ5LhaT1OcduaunjELMiN7OoOZ1fD.U05Vd.Am21/jeUu");

        map.put(user1.getUsername(),user1);
        map.put(user2.getUsername(),user2);
    }

    /**
     *
     * @param username 页面输入的用户名
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        initData();// 初始化数据
        //模拟查询数据库 根据用户名查询用户的基本信息（密码，用户的权限集合）
        User dbUser = map.get(username);
        if(dbUser == null){
            return null;//框架会处理
        }

        // <security:user name="admin" password="{noop}1234" authorities="checkitem_index,checkitem_add,checkitem_del"></security:user>
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("checkitem_index"));
        authorities.add(new SimpleGrantedAuthority("checkitem_add"));
        if(username.equals("admin")){
            authorities.add(new SimpleGrantedAuthority("checkitem_del"));
        }

        return new org.springframework.security.core.userdetails.User(username,dbUser.getPassword(),authorities);
    }

    public static void main(String[] args) {
//        String salt = "1231224535888123413213131231";
//        String pwd = "12345.&*&68433453454345fdfsdfdsgfgdsgds6" + salt;
//        String md5 = Md5.md5(pwd.getBytes());
//        System.out.println(md5);


//        //比对密码的过程
//        //$2a$10$/bTVvqqlH9UiE0ZJZ7N2Me3RIgUCdgMheyTgV0B4cMCSokPa.6oCa
//        String pwd = "123456" + "/bTVvqqlH9UiE0ZJZ7N2Me3";

        String pwd = "1234561";
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(pwd);
        System.out.println(encode);

        boolean matches1 = encoder.matches(pwd, "$2a$10$BTcW94KOLQ5LhaT1OcduaunjELMiN7OoOZ1fD.U05Vd.Am21/jeUu");
        boolean matches2 = encoder.matches(pwd, "$2a$10$AalikuzOiM2AwX4nLFFbw.4ACkWGgd2J5M0Wu9H5w7dPVL2tgiBOO");
        boolean matches3 = encoder.matches(pwd, "$2a$10$qvcNaK0D5g3x5.NajWFEr.l9hYmi7G2XbXybvcuO0Gm40VwUoCM4m");

        System.out.println(matches1);
        System.out.println(matches2);
        System.out.println(matches3);
    }
}
